package com.mmdccj.yugu.controller;

import com.github.pagehelper.PageInfo;
import com.mmdccj.yugu.common.Result;
import com.mmdccj.yugu.dto.CommentDto;
import com.mmdccj.yugu.entity.Comment;
import com.mmdccj.yugu.service.CommentService;
import com.mmdccj.yugu.utils.SnowFlakeUtil;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @Author: star_
 * @date: 2023-12-11 20:16
 */
@Slf4j
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    /**
     * 发送评论
     * Todo
     *
     * @param session session
     * @param comment 评论
     * @return Result<String>
     */
    @PostMapping("/sendComment")
    public Result<String> sendComment(HttpSession session, @RequestBody Comment comment) {
        String userId = (String) session.getAttribute("user");
        if (userId == null) {
            userId = "1";
        }
        log.info("userId:{}", userId);
        comment.setCreateTime(LocalDateTime.now());
        comment.setUserId(userId);
        comment.setId(SnowFlakeUtil.getSnowFlakeId().toString());
        if (comment.getCommentedId() == null)
            comment.setCommentedId("-1");
        commentService.insertComment(comment);
        log.info("{}创建评论...", userId);
        return Result.success("成功");
    }

    /**
     * 获取评论
     * Todo
     *
     * @param typeId
     * @param type
     * @param pageSize
     * @param currentPage
     * @param sortByLikes
     * @return Result<PageInfo < CommentDto>>
     */
    @GetMapping("/getComment")
    public Result<PageInfo<CommentDto>> getComment(String typeId, String type, Integer pageSize, Integer currentPage, boolean sortByLikes) {
        if (typeId == null || type == null || pageSize == null || currentPage == null) {
            return Result.error("缺少参数");
        }
        PageInfo<CommentDto> commentList = commentService.selectComments(type, typeId, currentPage, pageSize, sortByLikes);
        if (!commentList.hasContent()) {
            return Result.success("不存在评论", null);
        }

        return Result.success("成功", commentList);
    }

    /**
     * 点赞评论
     * Todo
     *
     * @param session session
     * @param postId  id
     * @return Result<String>
     */
    @PostMapping("/like")
    public Result<String> like(HttpSession session, String postId) {
        String userId = (String) session.getAttribute("user");
        if (userId == null) {
            return Result.error("未登录");
        }
        if (postId == null) {
            return Result.error("参数为空");
        }
        if (commentService.like(userId, postId)) {
            return Result.success("点赞成功");
        } else {
            return Result.error("已经点赞");
        }
    }

    /**
     * 在评论下评论
     */
    @PostMapping("/comment")
    public Result<String> comment(HttpSession session, @RequestBody Comment comment) {
        String userId = (String) session.getAttribute("user");
        if (userId == null) {
            return Result.error("未登录");
        }
        comment.setCreateTime(LocalDateTime.now());
        comment.setUserId(userId);
        comment.setId(SnowFlakeUtil.getSnowFlakeId().toString());
        if (comment.getCommentedId() == null)
            return Result.error("错误的参数");
        commentService.insertComment(comment);
        log.info("{}创建评论...", userId);
        return Result.success("成功");
    }
}
